﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestHttpInfras.RequestEntities;
using static System.Net.Mime.MediaTypeNames;

namespace RestHttpInfras;
public class HttpUtil : IHttpUtil
{

    /// <summary>
    /// 上传文件数据
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<bool> PostFileRequest(FileRequest request)
    {
        try
        {
            using var client = new HttpClient();

            // 序列化为JSON
            var json = JsonConvert.SerializeObject(request);

            // 获取字符串的字节数组
            var byteArray = Encoding.UTF8.GetBytes(json);

            var mbCount = byteArray.Length / 1024 / 1024;

            // 发送POST请求
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var response = await client.PostAsync("http://47.94.168.59:5010/upload", content);
            //var response = await client.PostAsync("http://127.0.0.1:5010/upload", content);

            // 确保响应成功
            response.EnsureSuccessStatusCode();

            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        

        //var responseBody = await response.Content.ReadAsStringAsync();
    }

    /// <summary>
    /// 设置分割
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<SegResponse> PostSegRequest(AiSegRequest request)
    {
        try
        {
            // 将对象序列化为JSON字符串
            var json = JsonConvert.SerializeObject(request);

            // 创建HttpContent对象
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            using var client = new HttpClient();

            var response = await client.PostAsync("http://47.94.168.59:5010/process", content);

            response.EnsureSuccessStatusCode();

            var jsonString = await response.Content.ReadAsStringAsync();

            var segRes = JsonConvert.DeserializeObject<SegResponse>(jsonString);

            return segRes;
        }
        catch (Exception e)
        {
            return new SegResponse();
        }
       
    }


    /// <summary>
    /// 设置分割
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<SegResponseNew> PostSegRequestNew(AiSegRequest request)
    {
        try
        {
            // 将对象序列化为JSON字符串
            var json = JsonConvert.SerializeObject(request);

            // 创建HttpContent对象
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            using var client = new HttpClient();

            var response = await client.PostAsync("http://47.94.168.59:5010/process", content);

            response.EnsureSuccessStatusCode();

            var jsonString = await response.Content.ReadAsStringAsync();

            var segRes = JsonConvert.DeserializeObject<SegResponseNew>(jsonString);

            return segRes;
        }
        catch (Exception e)
        {
            return new SegResponseNew();
        }

    }

}